package cn.aran.pet.controller;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.utils.LoginContext;
import cn.aran.org.domain.Employee;
import cn.aran.org.service.IEmployeeService;
import cn.aran.pet.domain.PetDetail;
import cn.aran.pet.service.IPetService;
import cn.aran.pet.domain.Pet;
import cn.aran.pet.query.PetQuery;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import cn.aran.user.domain.Logininfo;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest req){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(req);
            // 1.根据LogininfoId查询employee的shopId，再设置到pet的shopId
            Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
            pet.setShopId(employee.getShopId());
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

	
    /**
    * 查询单个
    * @Parameters: [id]
    * @Return
    */
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.queryData(query);
    }

    /**
    * @Title: onsale
    * @Description: 批量上架
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/17 0:28
    * @Parameters: [ids]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: offsale
    * @Description: 批量下架
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/17 0:29
    * @Parameters:
    * @Return
    */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /** 
    * @Title: getDetail
    * @Description: 详情
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/17 1:01  
    * @Parameters: [id]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @GetMapping ("/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id")Long id){
        try {
            PetDetail detail = petService.getDetail(id);
            return AjaxResult.me().setResultObj(detail);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
